<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree ref="tree" :data="jurisdictionList" node-key="id" :props="{label:'name'}" show-checkbox default-expand-all :check-strictly="true" />

    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="YesFn">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 引入获取权限接口
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
import { getRoleMessageAPI, assignPerm } from '@/api/setting'
// 引入转换属性结构方法
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 储存权限列表数据
      jurisdictionList: [],
      //   角色id
      id: ''
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 获取权限列表
    async getPermission() {
      this.jurisdictionList = listToTreeData(await getPermissionList())
    },
    // 关闭弹窗事件
    closeAssignDialog() {
      this.$emit('update:showAssignDialog', false)
    },
    // 回显个人角色
    async showOriginalFn(id) {
      this.id = id
      const { permIds } = await getRoleMessageAPI(id)
      console.log('回显角色', permIds)
      this.$refs.tree.setCheckedKeys(permIds)
    },
    // 确认按钮
    async YesFn() {
      await assignPerm({ id: this.id, permIds: this.$refs.tree.getCheckedKeys() })
      this.closeAssignDialog()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
